package com.dinghan.qccproject.controller;

import com.dinghan.qccproject.pojo.QccInfo;
import com.dinghan.qccproject.service.ExcelService;
import com.dinghan.qccproject.service.QccInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;
import java.util.stream.Collectors;

@Controller
public class ExcelController {

    @Autowired
    private ExcelService excelService;

    @Autowired
    QccInfoService qccInfoService;

    @GetMapping("/index")
    public String index() {
        return "index";
    }

    @PostMapping(value = "/upload")
    @ResponseBody
    public String uploadExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile file = multipartRequest.getFile("file");
        if (file.isEmpty()) {
            return "文件不能为空";
        }
        InputStream inputStream = file.getInputStream();
        List<QccInfo> notExistCellList = excelService.getNotExistCell(inputStream, file.getOriginalFilename());
        List<String> collect = notExistCellList.stream().map(qcc -> qcc.getTel()).collect(Collectors.toList());
        excelService.uploadExcelAboutUser(response, "apply.xlsx", null, collect);
        return "上传成功";
    }

}
